import { Injectable } from '@angular/core';
import { Action, State, StateContext } from '@ngxs/store';
import { SaveAction } from '.';
import { ElectronService } from '../../shared/electron/electron.service';
import { patch } from '@ngxs/store/operators';
import { NzMessageService } from 'ng-zorro-antd/message';
import * as immutable from 'immutable';
import * as _ from 'lodash';

export interface SaveStateModel {
  data: any;
}

@State<SaveStateModel>({
  name: 'save',
  defaults: {
    data: {},
  },
})
@Injectable({
  providedIn: 'root',
})
export class SaveState {

  constructor(
      private electronService: ElectronService,
      private message: NzMessageService,
  ) {
  }

  @Action(SaveAction.Load)
  loadSave(ctx: StateContext<SaveStateModel>) {
    if (this.electronService.isElectron) {
      let homePath = `${this.electronService.homePath}\\AppData\\LocalLow\\SanJiaoMao\ Studio\\GuaJiShenHua\\SaveDate1.text`;
      let saveData = this.electronService.read(homePath);
      if (!!saveData) {
        ctx.patchState({data: JSON.parse(saveData)});
      }
      this.message.success(`存档加载成功`);
    }
  }

  @Action(SaveAction.Save)
  save(ctx: StateContext<SaveStateModel>) {
    if (this.electronService.isElectron) {
      let homePath = `${this.electronService.homePath}\\AppData\\LocalLow\\SanJiaoMao\ Studio\\GuaJiShenHua\\SaveDate1.text`;
      let data = ctx.getState().data;
      this.electronService.write(homePath, JSON.stringify(data));
      this.message.success(`存档保存完成`);
    }
  }

  @Action(SaveAction.LockItem)
  LockItem(ctx: StateContext<SaveStateModel>, data: SaveAction.LockItem) {
    let data1: any = ctx.getState().data;
    let flag;
    if (0 === data.payload['物品类型']) {
      flag = '玩家武器';
    } else if (1 === data.payload['物品类型']) {
      flag = '玩家战衣';
    } else if (2 === data.payload['物品类型']) {
      flag = '玩家法宝';
    } else if (3 === data.payload['物品类型']) {
      flag = '玩家器魂';
    } else if (5 === data.payload['物品类型']) {
      flag = '玩家天材地宝';
    } else {
      return;
    }
    for (let i in data1[flag]) {
      const a = data1[flag][i];
      if (a['ID'] === data.payload['ID']) {
        ctx.setState(immutable.setIn(ctx.getState(),
            ['data', flag, i, '锁定状态'],
            a['锁定状态'] === 1 ? 0 : 1,
        ));
        return;
      }
    }
  }

  @Action(SaveAction.ChangeIntelligenceQuantity)
  changeIntelligenceQuantity(ctx: StateContext<SaveStateModel>, data: SaveAction.ChangeIntelligenceQuantity) {
    let old = ctx.getState().data['秘境情报'];
    ctx.setState(patch({
      data: patch({
        '秘境情报': old + data.payload,
      }),
    }));
  }

  @Action(SaveAction.SellItem)
  sellItem(ctx: StateContext<SaveStateModel>, data: SaveAction.SellItem) {
    ctx.setState(immutable.setIn(ctx.getState(),
        ['data', '灵炁数量'],
        ctx.getState().data['灵炁数量'] + data.payload['价值'],
    ));
    let flag;
    if (0 === data.payload['物品类型']) {
      flag = '玩家武器';
    } else if (1 === data.payload['物品类型']) {
      flag = '玩家战衣';
    } else if (2 === data.payload['物品类型']) {
      flag = '玩家法宝';
    } else if (3 === data.payload['物品类型']) {
      flag = '玩家器魂';
    } else if (5 === data.payload['物品类型']) {
      flag = '玩家天材地宝';
    }
    if (!!flag) {
      const idx = _(ctx.getState().data[flag]).findIndex(s => s['ID'] === data.payload['ID']).valueOf();
      ctx.setState(immutable.removeIn(ctx.getState(),
          ['data', flag, idx],
      ));
    }
  }

  @Action(SaveAction.ChangeMoney)
  changeMoney(ctx: StateContext<SaveStateModel>, data: SaveAction.ChangeMoney) {
    let old = ctx.getState().data['灵炁数量'];
    ctx.setState(patch({
      data: patch({
        '灵炁数量': old + data.payload * 100000000,
      }),
    }));
  }

  @Action(SaveAction.AddDragonSoul)
  addDragonSoul(ctx: StateContext<SaveStateModel>, data: SaveAction.AddDragonSoul) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < data.payload; i++) {
      list.push({
        'ID': count++,
        '名称': '龙魂',
        '价值': 15000000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 6,
        '等级': 1,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 6.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 2,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddTreasure)
  addTreasure(ctx: StateContext<SaveStateModel>, data: SaveAction.AddTreasure) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < data.payload; i++) {
      list.push({
        'ID': count++,
        '名称': '天材地宝',
        '价值': 10000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 4,
        '等级': 999,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 4,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0,
        '灵材类型': 0,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddAttackMedical)
  addAttackMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddAttackMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '攻击丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 300,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 3,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddDefenseMedical)
  addDefenseMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddDefenseMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '防御丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 300,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 4,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddCriticalMedical)
  addCriticalMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddCriticalMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '会心丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 300,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 5,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddHealthMedical)
  addHealthMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddHealthMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '体力丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 300,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 6,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddSpeedMedical)
  addSpeedMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddSpeedMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '速度丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 300,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 7,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddCleanMedical)
  addCleanMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddCleanMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '洗点丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 1,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 1,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddFaceMedical)
  addFaceMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddFaceMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '改颜丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 300,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 8,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddBodyMedical)
  addBodyMedical(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddBodyMedical) {
    let d = ctx.getState().data;
    let count = d['物品_ID生成器'];
    let list = [];
    for (let i = 0; i < payload; i++) {
      list.push({
        'ID': count++,
        '名称': '塑形丹',
        '价值': 200000,
        '物品类型': 5,
        '武器类型': 0,
        '战衣类型': 0,
        '品质': 5,
        '等级': 300,
        '固定属性链表': [],
        '随机属性链表': [],
        '排序权重': 5.0,
        '锁定状态': 0,
        '初次入界': false,
        '装备界_当前经验': 0,
        '装备界_所需经验': 0,
        '强度': 0.0,
        '灵材类型': 9,
        '基础套装效果': [],
        '套装终极效果': 0,
        '温养次数': 0,
      });
    }
    ctx.setState(patch({
      data: patch({
        '物品_ID生成器': count,
        '玩家天材地宝': [
          ...d['玩家天材地宝'],
          ...list,
        ],
      }),
    }));
  }

  @Action(SaveAction.AddSoul1)
  AddSoul1(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddSoul1) {
    let data = ctx.getState().data;
    const idx = _.findIndex(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 1);
    const find = _.find(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 1);
    ctx.patchState(immutable.setIn(ctx.getState(), ['data', '神识链表', idx, '数量'], payload + find['数量']));
  }

  @Action(SaveAction.AddSoul2)
  AddSoul2(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddSoul2) {
    let data = ctx.getState().data;
    const idx = _.findIndex(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 2);
    const find = _.find(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 2);
    ctx.patchState(immutable.setIn(ctx.getState(), ['data', '神识链表', idx, '数量'], payload + find['数量']));
  }

  @Action(SaveAction.AddSoul3)
  AddSoul3(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddSoul3) {
    let data = ctx.getState().data;
    const idx = _.findIndex(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 3);
    const find = _.find(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 3);
    ctx.patchState(immutable.setIn(ctx.getState(), ['data', '神识链表', idx, '数量'], payload + find['数量']));
  }

  @Action(SaveAction.AddSoul4)
  AddSoul4(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddSoul4) {
    let data = ctx.getState().data;
    const idx = _.findIndex(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 4);
    const find = _.find(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 4);
    ctx.patchState(immutable.setIn(ctx.getState(), ['data', '神识链表', idx, '数量'], payload + find['数量']));
  }

  @Action(SaveAction.AddSoul5)
  AddSoul5(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddSoul5) {
    let data = ctx.getState().data;
    const idx = _.findIndex(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 5);
    const find = _.find(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 5);
    ctx.patchState(immutable.setIn(ctx.getState(), ['data', '神识链表', idx, '数量'], payload + find['数量']));
  }

  @Action(SaveAction.AddSoul6)
  AddSoul6(ctx: StateContext<SaveStateModel>, {payload}: SaveAction.AddSoul6) {
    let data = ctx.getState().data;
    const idx = _.findIndex(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 6);
    const find = _.find(data['神识链表'], (s: any) => s['品质'] === 6 && s['种族'] === 6);
    ctx.patchState(immutable.setIn(ctx.getState(), ['data', '神识链表', idx, '数量'], payload + find['数量']));
  }
}
